home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Utilities / Programming / EnterAct 3.5 / Drag_on Modules / hAWK programs / $Print_STR# < prev    next >
Encoding:
Text File  |  1993-04-09  |  4.1 KB  |  158 lines  |  [TEXT/KEEN]

  1. #$Print_STR#: as loaded by ReadResource
  2.  
  3. #The approach below uses "input on demand" to allow us
  4. #to step through the bytes in the resource. Each call to
  5. #GetNextByte or PrintCharAndAdvance retrieves the next byte, grabbing
  6. #the next input line if necessary. The variable bytePos is
  7. #automatically advanced whenever a byte is read, and the calculation
  8. #of the field number and position within the field is hidden within
  9. #GetNextByte. To skip over n bytes, add n to bytePos.
  10.  
  11. #Format of STR# resource:
  12. #2        number of strings (numStrings below)
  13. #--repeated for numStrings:
  14. #1        length of string (stringLen below)
  15. #n        string text, stringLen bytes
  16.  
  17. #Format of resource as retrieved by Read Resource:
  18. #byteNumber: 4bytes 4bytes 4bytes 4bytes  ascii version of the bytes
  19. #the last line may be padded with zeroes, for example
  20. #      240: 07556E6D 61726BC9 00000000 09417574  .Unmark......Aut
  21. #      256: 6F6D6172 6BC90000 00000000 00000000  omark......
  22.  
  23. #bytePos ranges 0 : up. It is automatically
  24. #advanced when a byte is read by GetNextByte(). It can be
  25. #manually advanced to skip unwanted bytes.
  26.  
  27. # User’s Manual references:
  28. # «hAWK User’s Manual» «F   Running hAWK programs»
  29. # «hAWK User’s Manual» «L  5   Regular expressions»
  30. # «hAWK User’s Manual» «M  5   Built-in string and file functions»
  31. # «hAWK User’s Manual» «K  4   Built-in variables»
  32. # «hAWK User’s Manual» «K  8   Arrays»
  33. # «hAWK User’s Manual» «N   User-defined functions»
  34. # «hAWK User’s Manual» «P  3   The getline function»
  35. # «hAWK User’s Manual» «O  3   Output into files»
  36. # «hAWK User’s Manual» «Q   The hAWK function»
  37.  
  38. FNR == 1    {
  39.             numStrings = GetNextBytesAsNumber(2)
  40.             print "Number of strings:", numStrings;
  41.             while (1)
  42.                 {
  43.                 stringLen = GetNextByte()
  44.                 printf("%3d ", stringLen);
  45.                 while (stringLen > 0)
  46.                     {
  47.                     PrintCharAndAdvance();
  48.                     --stringLen;
  49.                     }
  50.                 print ""
  51.                 }
  52.             }
  53.  
  54.  
  55.  
  56. function PrintCharAndAdvance()
  57.     {
  58.     printf("%c", GetNextByte());
  59.     }
  60.  
  61. #Convert ASCII representation of next byte, eg '1F', to a single
  62. #number, eg 31.
  63. function GetNextByte(        lineByte, field, char, pos)
  64.     {
  65.     #Convert byte position in resource to field and position in field.
  66.     lineByte = bytePos - (FNR - 1) * 16;
  67.     field = int((lineByte)/4);
  68.     while (field > 3)#the next line of input is wanted
  69.         {
  70.         if ((getline) <= 0)
  71.             {
  72.             print ""; print "End of file."; exit;
  73.             }
  74.         lineByte = bytePos - (FNR - 1) * 16;
  75.         field = int((lineByte)/4);
  76.         }
  77.     pos = lineByte - field * 4;
  78.     field += 2;
  79.     pos = 2 * pos + 1;
  80.     char = HexToDec(substr($field, pos, 2));
  81.     ++bytePos;
  82.     return char
  83.     }
  84.  
  85. #Return raw ASCII representation of byte, eg "2F".
  86. function GetNextRawByte(        lineByte, field, char, pos)
  87.     {
  88.     #Convert byte position in resource to field and position in field.
  89.     lineByte = bytePos - (FNR - 1) * 16;
  90.     field = int((lineByte)/4);
  91.     while (field > 3)#the next line of input is wanted
  92.         {
  93.         if ((getline) <= 0)
  94.             {
  95.             print ""; print "End of file."; exit;
  96.             }
  97.         lineByte = bytePos - (FNR - 1) * 16;
  98.         field = int((lineByte)/4);
  99.         }
  100.     pos = lineByte - field * 4;
  101.     field += 2;
  102.     pos = 2 * pos + 1;
  103.     ++bytePos;
  104.     return substr($field, pos, 2);
  105.     }
  106.  
  107. #Convert ASCII representation of 1, 2, or 4 bytes to a single
  108. #unsigned number.
  109. function GetNextBytesAsNumber(howManyBytes,        i, temp, numString, number)
  110.     {
  111.     for (i = 1; i <= howManyBytes; ++i)
  112.         {
  113.         temp = GetNextRawByte();
  114.         numString = numString temp;
  115.         }
  116.     number = HexToDec(numString);
  117.     return number;
  118.     }
  119.  
  120. #General purpose ASCII hex to number conversion.
  121. function HexToDec(h,     len, nyb, hexDigit, power, d)
  122.     {
  123.     d = 0;
  124.     len = length(h);
  125.     power = 1
  126.     for (nyb = len; nyb >= 1; --nyb)
  127.         {
  128.         hexDigit = substr(h, nyb, 1);
  129.         d += DecimalEquiv(hexDigit) * power;
  130.         power *= 16;
  131.         }
  132.     return d
  133.     }
  134.  
  135. #Convert one hex "digit" to the equivalent number.
  136. function DecimalEquiv(hd, dd)
  137.     {
  138.     dd = 0;
  139.     if (match(hd,/[0-9]/) > 0)
  140.         dd = hd + 0;#conversion from string to number is implicit
  141.     else if (match(hd, /[a-fA-F]/) > 0)
  142.         {
  143.         if (match(hd, /[aA]/) > 0)
  144.             dd = 10;
  145.         else if (match(hd, /[bB]/) > 0)
  146.             dd = 11;
  147.         else if (match(hd, /[cC]/) > 0)
  148.             dd = 12;
  149.         else if (match(hd, /[dD]/) > 0)
  150.             dd = 13;
  151.         else if (match(hd, /[eE]/) > 0)
  152.             dd = 14;
  153.         else if (match(hd, /[fF]/) > 0)
  154.             dd = 15;
  155.         }
  156.     return dd
  157.     }
  158.